package com.yj.suncity.viewpager;

import java.util.List;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup.LayoutParams;
import android.view.ViewGroup;

import android.widget.TextView;

import com.yj.suncity.R;
import com.yj.suncity.activity.shouye.bean.ShouyePlacardBeans;
import com.yj.suncity.setting.HelpActivity;
import com.yj.suncity.utis.PagerAdapter;
import com.yj.suncity.utis.VerticalViewPager;
import com.yj.suncity.utis.VerticalViewPager.OnPageChangeListener;

public class VerticalViewPagerScroll {

	private VerticalViewPager pager;
	private int currentItem = 0; // 当前图片的索引号
	private int oldIndex = 0;//
	private List<ShouyePlacardBeans> list;
	private ScheduledExecutorService scheduledExecutorService;
	private Context context;

	/**
	 * 自滚动的ViewPager
	 * 
	 * @param context
	 * @param pager
	 * @param list
	 */
	public VerticalViewPagerScroll(Context context, VerticalViewPager pager,
			List<ShouyePlacardBeans> list) {
		super();
		this.pager = pager;
		this.list = list;
		this.context = context;
		setViewPager();

	}

	private void setViewPager() {

		pager.setAdapter(new MyPagerAdapter());

		pager.setOnPageChangeListener(new OnPageChangeListener() {

			@Override
			public void onPageSelected(int arg0) {
				//
				currentItem = arg0 % list.size();
				oldIndex = currentItem;
			}

			@Override
			public void onPageScrolled(int arg0, float arg1, int arg2) {

			}

			@Override
			public void onPageScrollStateChanged(int arg0) {
			}
		});
		//
		pager.setOnTouchListener(new OnTouchListener() {
			public boolean onTouch(View v, MotionEvent event) {
				if (event.getAction() == MotionEvent.ACTION_UP) {
					startTimer();
				} else {
					stopTimer();
				}
				return false;
			}
		});

	}

	public void startTimer() {
		if (scheduledExecutorService == null
				|| scheduledExecutorService.isShutdown()) {
			scheduledExecutorService = Executors
					.newSingleThreadScheduledExecutor();
			// 当Activity显示出来后，每3秒钟切换一次图片显示
			scheduledExecutorService.scheduleAtFixedRate(new ScrollTask(), 1,
					3, TimeUnit.SECONDS);
		}
	}

	public void stopTimer() {
		// 当Activity不可见的时候停止切换
		if (scheduledExecutorService != null
				&& !scheduledExecutorService.isShutdown()) {
			scheduledExecutorService.shutdown();
			scheduledExecutorService = null;
		}
	}

	// 切换当前显示的图片
	private Handler handler = new Handler() {
		public void handleMessage(Message msg) {
			pager.setCurrentItem(currentItem);// 切换当前显示的图片
		};
	};

	/**
	 * 换行切换任务
	 * 
	 * @author Administrator
	 */
	private class ScrollTask implements Runnable {

		public void run() {
			synchronized (pager) {

				// if (pager) {
				// if (currentItem <= 1) {
				// pager = false;
				// }
				// currentItem = (currentItem - 1) % list.size();
				// handler.obtainMessage().sendToTarget(); // 通过Handler切换图片
				// } else {
				// if (currentItem >= (list.size() - 2)) {
				// pager = true;
				// }
				// currentItem = (currentItem + 1) % list.size();
				// handler.obtainMessage().sendToTarget(); // 通过Handler切换图片
				// }
				//
				if (currentItem >= list.size()) {
					currentItem = 0;
				}
				currentItem = (currentItem + 1) % list.size();
				handler.obtainMessage().sendToTarget(); // 通过Handler切换图片
			}
		}

	}

	class MyPagerAdapter extends PagerAdapter {
		@Override
		public int getCount() {
			return list.size();
		}

		@Override
		public void destroyItem(ViewGroup container, int position, Object object) {
			((VerticalViewPager) container).removeView((View) object);
			// super.destroyItem(container, position, object);
		}

		@Override
		public Object instantiateItem(ViewGroup container, int position) {
			TextView tv = new TextView(context);
			final ShouyePlacardBeans  beas = list.get(position);
			tv.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
					LayoutParams.MATCH_PARENT));
			tv.setGravity(Gravity.CENTER_VERTICAL);
			tv.setTextSize(15);
			tv.setTextColor(context.getResources().getColor(R.color.red));
			tv.setText(beas.getTitle());
			tv.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					 Intent intent = new Intent(context, HelpActivity.class);
					 intent.putExtra("content", beas.getContent());
					 intent.putExtra("flag", 1);
					 context.startActivity(intent);
				}
			});
			
			container.addView(tv);
			return tv;

			// ((VerticalViewPager) container).addView(imageLayout, 0); //
			// 将图片增加到ViewPager
			// return imageLayout;
		}

		@Override
		public boolean isViewFromObject(View arg0, Object arg1) {
			return arg0 == arg1;
		}
	}

}
